package com.graduation.smartparkingplatform.Controller;

import com.github.pagehelper.PageInfo;
import com.graduation.smartparkingplatform.Pojo.Charging;
import com.graduation.smartparkingplatform.Response.Response;
import com.graduation.smartparkingplatform.Response.ResponseUtils;
import com.graduation.smartparkingplatform.Service.ChargingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/charging")
public class ChargingController {
  @Autowired ChargingService chargingService;

  // 获取充电停车位列表(分页)
  @GetMapping("/chargingList")
  public Response getChargingList(
      @RequestParam(value = "page", defaultValue = "1") Integer page,
      @RequestParam(value = "count", defaultValue = "10") Integer count) {
    try {
      PageInfo<Charging> chargingList = chargingService.getChargingList(page, count);
      return ResponseUtils.buildSuccess(chargingList);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }

  // 精确id搜索
  @GetMapping("/searchCharging")
  public Response searchCharging(
      @RequestParam(value = "spaceNo", defaultValue = "0") Integer spaceNo) {
    try {
      Charging searchList = chargingService.searchCharging(spaceNo);
      return ResponseUtils.buildSuccess(searchList);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }

  // 修改充电信息
  @PostMapping("/changeInfo")
  public Response changeInfo(@RequestBody Charging charging) {
    System.out.println("charging = " + charging.toString());
    Boolean changeInfo = chargingService.changeInfo(charging);
    if (changeInfo) {
      return ResponseUtils.buildSuccess("修改充电信息成功");
    }
    return ResponseUtils.buildError("修改充电信息失败");
  }
}
